package weblab.tutor;

import utils.QuizTasks;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class WebQuizInterface {
	
    Context mContext;   
    String lesson_id = "";
    String questionFormat = "";

    /** Instantiate the interface and set the context */
    WebQuizInterface(Context c,String s,String text) {
        mContext = c;
        lesson_id = s;
        this.questionFormat = text;
    }

    /** Show a toast from the web page */
    //@JavascriptInterface
    
    public String getQuestions() {
    	String text = "";
    	try{
    		Toast.makeText(mContext, "Retrieving questions", Toast.LENGTH_SHORT).show();
    		text = new QuizTasks().execute("get_questions",lesson_id, questionFormat).get();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    	return text;
    }
    
    public String getQuizes() {
    	String text = "";
    	try{
    		Toast.makeText(mContext, "Retrieving quizes", Toast.LENGTH_SHORT).show();
    		text = new QuizTasks().execute("get_quizes").get();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    	return text;
    }
    
    public String validate(String [] options){
    	String answers = "";
    	for(String o:options)
    		if(o.contains("1"))
    			answers += "a ";
    		else
    			if(o.contains("2"))
    				answers += "b ";
    			else
    				if(o.contains("3"))
        				answers += "c ";
    				else
    	    			answers += "d ";
    	String text = "";
    	try{
    		Toast.makeText(mContext, "Retrieving answers", Toast.LENGTH_SHORT).show();
    		text = new QuizTasks().execute("get_questions",lesson_id, questionFormat,"validate_answers", answers).get();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    	return text;
    }
    
    public String loadValidation(){
    	String text = "";
    	try{
    		Toast.makeText(mContext, "Retrieving answers", Toast.LENGTH_SHORT).show();
    		text = new QuizTasks().execute("get_validation").get();
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    	return text;
    }
    
    public void redirect(String lesson_id){
    	Intent i = new Intent(mContext,LessonsActivity.class);
    	i.putExtra("lesson_id", lesson_id);
    	mContext.startActivity(i);
    }
    
    public void quizRedirect(String lesson_id){
    	Intent i = new Intent(mContext,QuizActivity.class);
    	i.putExtra("lesson_id", lesson_id);
    	mContext.startActivity(i);
    }
    
    public void quizListRedirect(){
    	Intent i = new Intent(mContext,QuizListActivity.class);
    	mContext.startActivity(i);
    }
}
